<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	<style>
		
	</style>
  </head>
  	
  		
  <body>
  <div class="easyui-layout" fit="true">
    <!-- 表格数据区域 -->
		<div region="center" border="true" data-optinos="fit:true;" style="">
			<table id="haipumi_tables" style="float:left;margin-left:20px;margin-top:20px;">
				<!-- <c:forEach items="${sessionScope.tables}" varStatus="i" var="item" > 
					<tr style="padding-left:2px;">
						<td style="width:100px; border-bottom:1px blue solid;text-align: center">
							${i.index+1}
						</td>
						<td style="width:200px;">
							${item.tables_in_haipumi }
						</td>
						<td>
							<a class="backupbuttom" href="javascript:backup('${item.tables_in_haipumi }',${i.index+1})">备份</a>|<a class="restorebuttom" href="javascript:showrestore('${item.tables_in_haipumi }')">还原</a>|<a class="showbutotm" href="javascript:showbackup('${item.tables_in_haipumi }')">查看备份</a>
						</td>
					</tr>
				</c:forEach> -->
			</table>
			<div style="width:300px;height:300px; background:#999; float:right; margin-right:50px; margin-top:20px;">
				<center id="databaseinfo">
					<h2>数据库管理注意事项</h2>
					<p>…………………………<br/>………………………………<br/>………………………………<br/>………………………………<br/>………………………………<br/>
						………………………………<br/>………………………………<br/>………………………………<br/>………………………………</p>
					<a id="btn" href="javascript:backupall()" class="easyui-linkbutton" data-options="iconCls:'icon-search',border:false" >备份haipumi数据库</a> 
					
				</center>
			</div>
		</div>
		
	</div>
	
	<div id="showdatabasedialog" class="easyui-dialog" title="备份文件列表" databasename="" style="width:400px;height:400px;"
			data-options="iconCls:'pag-list',modal:true,closable:false,collapsible:false,minimizable:false,maximizable:false,resizable:false,closed:true">
  		<center>
	  		<div id="yhj_showDatabase" style="width:400px;height:300px;margin-top:20px;">
	  		
	  		</div>
	  	</center>
  		<div style="text-align:right;margin-right:30px;">
			<a href="javascript:cancel('showdatabasedialog')" class="easyui-linkbutton" data-options="iconCls:'icon-undo'">确定</a>
		</div>
  	</div>
	
	<div id="databaseuploadDialog" class="easyui-dialog" databasename="" title="上传备份sql文件" style="width:400px;height:100px;"
			data-options="iconCls:'pag-list',modal:true,closable:false,collapsible:false,minimizable:false,maximizable:false,resizable:false,closed:true">
  		<input id="databaseuploadfile" type="file"/>
  		<div style="text-align:right;margin-right:30px;">
			<a href="javascript:restore()" class="easyui-linkbutton" data-options="iconCls:'icon-ok'">确定</a>
			<a href="javascript:cancel('databaseuploadDialog')" class="easyui-linkbutton" data-options="iconCls:'icon-undo'">取消</a>
		</div>
  	</div>
  	<script>
  		
  		
  		
  		function showbackup(databasename){
  			//alert(databasename);
  			$("#databaseuploadDialog").attr("databasename",databasename);
  			$("#showdatabasedialog").window("open");
  			$.ajax({
  				url:"backadmin/database.action?op=showdatabasebackup",
	  			type:"POST",
	  			dataType:"JSON",
	  			data:{"tablename":databasename},
	  			success:function(data){
	  				//alert(data);
	  				if(data.code==1&&data.obj!=null&&data.obj!=""){
	  					$("#yhj_showDatabase").html("");
	  					for(var i=0;i<data.obj.length;i++){
	  						$("#yhj_showDatabase").append("<a href='javascript:void(0)' style='font-size:14px;' onClick='restorefromweb(\""+data.obj[i]+"\")'>"+data.obj[i]+"</a><br/>");
	  					}
	  				}else{
	  					$("#yhj_showDatabase").html("查无此备份");
	  					//alert(data.msg);
	  				}
	  			}
  			});
  		}
  	
  		function cancel(name){
  			if(name=='databaseuploadDialog'){
  				$("#databaseuploadDialog").window("close");
  			}else if(name=='showdatabasedialog'){
  				$("#showdatabasedialog").window("close");
  			}
  		}
  	
  		function backupall(){
  			//alert("asdf");
  			$.ajax({
	  			url:"backadmin/database.action?op=backupall",
	  			type:"POST",
	  			dataType:"JSON",
	  			success:function(data){
	  				if(data.code==1){
	  					showinfobox(1,"","haipumi数据库已全部备份.需下载可点击页面下载按钮...");
	  					$("#databaseinfo").append("<br/><br/><a href='"+data.msg+"' style=\"color:#fff;font-size:18px;text-decoration:none;\">点击下载最新全部数据库备份</a>");
	  				}else{
	  					showinfobox(1,"","haipumi数据库b备份失败，请联系管理员...");
	  				}
	  			}
  			});
  		}
  		
  		function restorefromweb(name){
  			alert(name);
  			$.ajax({
	  			url:"backadmin/database.action?op=restorefromweb",
	  			type:"POST",
	  			dataType:"JSON",
	  			data:{"tablename":name},
	  			success:function(data){
	  				
	  			}
  			});
  		}
  	
		function backup(name,index){
			//alert(name);
			$.ajax({
	  			url:"backadmin/database.action?op=backup",
	  			type:"POST",
	  			dataType:"JSON",
	  			data:{"tablename":name},
	  			success:function(data){
	  				if(data.code==1){
	  					//alert("已经备份");
	  					showinfobox(1,"","已经成功保存到服务器，如需下载，请点击下载按钮...");
	  					//$("#backupbuttom").attr("href",data.msg);
	  					//$("#backupbuttom").text("右键另存为");
	  					 
	  					//alert(index);
	  					$(".backupbuttom").eq(index-1).attr("href",data.msg);
	  					$(".backupbuttom").eq(index-1).text("右键另存为");
	  				}if(data.code==0){
	  					showinfobox(1,"",data.msg);
	  				}
	  			}
	  		});
		}  	
		
		function showrestore(databasename){
			//先验证文件的存在，然后存到文件夹中，再读取执行
			$("#databaseuploadDialog").attr("databasename",databasename);
			$("#databaseuploadDialog").window("open");
		}  	
		function restore(){
			var file=$("#databaseuploadfile").val();
			if(file==""){
				alert("上传sql文件不能为空...");
				return;
			}
			$.ajaxFileUpload({
  				url:"backadmin/file.action",//这个action主要用来接收上传的文件
  				secureuri:false,//上传的时候是否使用安全协议https而不用http
  				type:"POST",
  				dataType:"JSON",
  				fileElementId:"databaseuploadfile",//上传文件域的id
  				data:{op:'databaserestore',databasename:$("#databaseuploadDialog").attr("databasename"),addrDir:'/haipumi_database_backup'},
  				success:function(data){
  					if(data!=""){
  						cancel('databaseuploadDialog');
  					}else{
  						//失败了
  						cancel('databaseuploadDialog');
  					}
  				}
  			});
		}
  	
	  	$(function(){
	  		$.ajax({
	  			url:"backadmin/database.action?op=findalltable",
	  			type:"POST",
	  			datatype:"JSON",
	  			success:function(data){
	  				/*<c:forEach items="${sessionScope.tables}" varStatus="i" var="item" > 
					<tr style="padding-left:2px;">
						<td style="width:100px; border-bottom:1px blue solid;text-align: center">
							${i.index+1}
						</td>
						<td style="width:200px;">
							${item.tables_in_haipumi }
						</td>
						<td>
							<a class="backupbuttom" href="javascript:backup('${item.tables_in_haipumi }',${i.index+1})">备份</a>|<a class="restorebuttom" href="javascript:showrestore('${item.tables_in_haipumi }')">还原</a>|<a class="showbutotm" href="javascript:showbackup('${item.tables_in_haipumi }')">查看备份</a>
						</td>
					</tr>
					</c:forEach>*/
					var str="";
					for(var i=0;i<data.obj.length;i++){
						var tableObj=$("#haipumi_table");
						str+='<tr style="padding-left:2px;"><td style="width:100px; border-bottom:1px blue solid;text-align: center">'+(i+1)+'</td><td style="width:200px;">'+data.obj[i].tables_in_haipumi+'</td><td><a class="backupbuttom" href="javascript:backup(\''+data.obj[i].tables_in_haipumi+'\','+(i+1)+')">备份</a>|<a class="restorebuttom" href="javascript:showrestore(\''+data.obj[i].tables_in_haipumi+'\')">还原</a>|<a class="showbutotm" href="javascript:showbackup(\''+data.obj[i].tables_in_haipumi+'\')">查看备份</a></td></tr>';
					}
					$("#haipumi_tables").html(str);
	  			}
	  		});
	  	});
  	</script>
  </body>
</html>
